package dao;

import util.DBConn;
import util.UUIDUtil;
import vo.WareHouseDetail;
import vo.WareHouseUrls;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/4.
 */
public class CrawlerServiceImpl implements CrawlerService{
    /**
     * insert list:爬取目标数据的页面urllist
     * */
    public void insertUrls(List<WareHouseUrls> list){
        Connection conn = DBConn.getConn();
        PreparedStatement ps = null;
        //mysql查询语句
        StringBuffer sql = new StringBuffer("insert into warehouse_url_list(id,url,city,web,createtime) values ");
        for (WareHouseUrls wareHouseUrls : list) {
            sql.append("(");
            sql.append("'"+ UUIDUtil.getUUID()+"',");
            sql.append("'"+ wareHouseUrls.getUrl()+"',");
            sql.append("'"+ wareHouseUrls.getCity()+"',");
            sql.append("'"+ wareHouseUrls.getWeb()+"',");
            sql.append("'"+ wareHouseUrls.getCreatetime()+"'),");
        }
        sql.deleteCharAt(sql.length()-1);
        try {
            ps = conn.prepareStatement(sql.toString());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 改变解析状态
     * */
    public void updateUrlsStatus(){
        Connection conn = DBConn.getConn();
        PreparedStatement ps = null;
        //mysql查询语句
        StringBuffer sql = new StringBuffer("update warehouse_url_list set parseStatus=1 where parseStatus=0");
        try {
            ps = conn.prepareStatement(sql.toString());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * select list
     * */
    public List<WareHouseUrls> urlListToParse(){
        List<WareHouseUrls> list = new ArrayList<>();
        Connection conn = DBConn.getConn();
        PreparedStatement ps = null;
        //mysql查询语句
        StringBuffer sql = new StringBuffer("select id,url,parseStatus from warehouse_url_list  where parseStatus=0");
        try {
            ps = conn.prepareStatement(sql.toString());
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                WareHouseUrls u = new WareHouseUrls();
                    u.setUrl(rs.getString("url"));
                    u.setId(rs.getString("id"));
                list.add(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
    /**
     * insert detail
     * */
    public void insertDetail(WareHouseDetail d){
        Connection conn = DBConn.getConn();
        PreparedStatement ps = null;

        StringBuffer sql = new StringBuffer("insert into warehouse_detail")
                    .append("(id,warehouse_name,city_code,detail_address,warehouse_area,contact_name,contact_tel,")
                    .append("deleted,warehouse_discription,createtime,updatetime,imgHref) values ");
            sql.append("(");
            sql.append("'"+ UUIDUtil.getUUID()+"',");
            sql.append("'"+ d.getWarehouse_name()+"',");
            sql.append("'"+ d.getCity_code()+"',");
            sql.append("'"+ d.getDetail_address()+"',");
            sql.append("'"+ d.getWarehouse_area()+"',");
            sql.append("'"+ d.getContact_name()+"',");
            sql.append("'"+ d.getContact_tel()+"',");
            sql.append("'"+ d.getDelete()+"',");
            sql.append("'"+ d.getWarehouse_discription()+"',");
            sql.append("'"+ d.getCreatetime()+"',");
            sql.append("'"+ d.getUpdatetimes()+"',");
            sql.append("'"+ d.getImgHref()+"')");
        try {
            ps = conn.prepareStatement(sql.toString());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
